package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.GoodsDao;
import cn.gjculture.shopping.api.dao.HouseDao;
import cn.gjculture.shopping.api.entity.Commodity;
import cn.gjculture.shopping.api.entity.House;
import cn.gjculture.shopping.api.service.GoodsService;
import cn.gjculture.shopping.api.service.HouseService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    HouseDao houseDao;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;
    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsDao goodsDao;

    @Override
    public int insert(House house) {
        house.setGoodsId(UUIDUtil.getUUID());
        house.setInsertTime(DateUtil.getTimestamp());
        house.setUpdateTime(DateUtil.getTimestamp());
        house.setStatus(0);
        goodsService.insertThumbnail(house.getThumbnails(),house.getGoodsId());
        houseDao.insert(house);
        return 1;
    }

    @Override
    public List<House> query() {
        List<House> houses = houseDao.query();
        for(House house:houses){
            collect(house);
        }
        return houses;
    }

    private void collect(House house) {
        List<Commodity.Thumbnail> thumbnails = goodsDao.queryThumb(house.getGoodsId());
        house.setThumbnails(thumbnails);
    }

    @Override
    public House queryById(String goodsId) {
        House house = houseDao.queryById(goodsId);
        collect(house);
        return house;
    }

    @Override
    public int delete(List<String> goodsIds) {
        return commonDeleteMapper.deleteHouse(goodsIds);
    }

    @Override
    public int update(House house) {
        house.setUpdateTime(DateUtil.getTimestamp());
        goodsService.handleThumb(house.getGoodsId(),house.getThumbnails());
        return commonUpdateMapper.updateHouse(house);
    }

    @Override
    public List<House> queryByHotel(String hotelId) {
        List<House> houses = houseDao.queryByHotel(hotelId);
        for(House house:houses){
            collect(house);
        }
        return houses;
    }

}
